package com.llkj.contractmanagement.contractmanagement.mine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.llkj.contractmanagement.customview.MyDialog;
import com.llkj.contractmanagement.customview.flow.TagView;
import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.login.EditPassWordActivity;
import com.llkj.contractmanagement.contractmanagement.login.LoginActivity;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.model.AboutMeBean;
import com.llkj.contractmanagement.model.UpdateBean;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.AppManager;
import com.llkj.contractmanagement.utils.DownloadService;
import com.llkj.contractmanagement.utils.GsonUtil;
import com.llkj.contractmanagement.utils.ToastUtil;
import com.llkj.contractmanagement.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.HashMap;

import butterknife.Bind;
import cn.jpush.android.api.JPushInterface;

/**
 * author: dong.yj
 * created on: 2016/8/18 15:24
 * description: 设置界面
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener, MyDialog.ItemClickListener {
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting;
    }

    @Bind(R.id.rl_edit_password)
    RelativeLayout rlEditpw;
    @Bind(R.id.rl_kefu)
    RelativeLayout rlKefu;
    @Bind(R.id.rl_version_update)
    RelativeLayout rlVersionUp;
    @Bind(R.id.rl_feed_back)
    RelativeLayout rlFeedBack;
    @Bind(R.id.rl_delete_cache)
    RelativeLayout rlDeleteCache;
    @Bind(R.id.btn_logout)
    Button btnLogout;
    @Bind(R.id.title_setting)
    TitleBar titleBar;
    @Bind(R.id.tv_version_number)
    TextView tv_version_number;
    private String phoneNumber;
    private int type = 0;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
    }

    private void init() {
        HttpMethod.aboutMe(this, this, null, UrlConfig.HTTP_ABOUTME);
        setListener();
        tv_version_number.setText("V" + Utils.getVersionName(this));
    }

    private void setListener() {
        titleBar.setTopBarClickListener(this);
        rlEditpw.setOnClickListener(this);
        rlKefu.setOnClickListener(this);
        rlVersionUp.setOnClickListener(this);
        rlFeedBack.setOnClickListener(this);
        rlDeleteCache.setOnClickListener(this);
        btnLogout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.rl_edit_password:
                intent = new Intent(this, EditPassWordActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.rl_kefu:
                type = 1;
                MyDialog myDialog = new MyDialog(this, phoneNumber, R.style.MyDialogStyle1);
                myDialog.setItemClickListener(this);
                myDialog.setRightText("呼叫");
                myDialog.setLeftText("取消");
                myDialog.show();
                break;
            case R.id.rl_version_update://版本更新
                versionUpdate();
                break;
            case R.id.rl_feed_back:
                intent = new Intent(this, FeedBackActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_delete_cache:
                type = 2;
                MyDialog delcacheDialog = new MyDialog(this, "确定清除缓存吗", R.style.MyDialogStyle1);
                delcacheDialog.setItemClickListener(this);
                delcacheDialog.setRightText("确定");
                delcacheDialog.setLeftText("取消");
                delcacheDialog.show();
                break;
            case R.id.btn_logout:
                type = 3;
                MyDialog logout = new MyDialog(this, "确定要退出吗", R.style.MyDialogStyle1);
                logout.setItemClickListener(this);
                logout.setRightText("确定");
                logout.setLeftText("取消");
                logout.show();
                break;
        }
    }

    private void versionUpdate() {
        String version = Utils.getVersionName(this);
        HashMap<String, String> map = new HashMap<>();
        map.put("version", version);
        HttpMethod.version(this, this, map, UrlConfig.HTTP_VERSION);
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_ABOUTME://关于我们（在线客服电话号码）:
                AboutMeBean aboutMeBean = GsonUtil.GsonToBean(responseInfo, AboutMeBean.class);
                if (aboutMeBean != null) {
                    phoneNumber = aboutMeBean.getPhone();
                }
                break;
            case UrlConfig.HTTP_VERSION:    //版本更新
                UpdateBean updateBean = GsonUtil.GsonToBean(responseInfo, UpdateBean.class);
                if (updateBean.getState() == 1) {
                    if (!Utils.getVersionName(this).equals(updateBean.getTitle())) {
                        showUpdateDialog(updateBean.getContent(), "有新版本，请下载更新");
                    } else {
                        ToastUtil.makeShortText(this, "已是最新版本");
                    }
                }
                break;
        }
    }

    private void showUpdateDialog(final String url, String message) {
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("发现新版本")
                .setMessage(message)
                .setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(SettingActivity.this, DownloadService.class);
                        intent.putExtra("url", url);
                        startService(intent);
//                        Intent intent = new Intent();
//                        intent.setAction("android.intent.action.VIEW");
//                        Uri content_url = Uri.parse(url);
//                        intent.setData(content_url);
//                        startActivity(intent);
                    }
                })
                .setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }

    @Override
    public void left() {

    }

    @Override
    public void right() {
        if (type == 1) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            //url:统一资源定位符
            //uri:统一资源标示符（更广）
            intent.setData(Uri.parse("tel:" + phoneNumber));
            //开启系统拨号器
            startActivity(intent);
        } else if (type == 2) {
            ImageLoader.getInstance().clearDiskCache();
            ImageLoader.getInstance().clearMemoryCache();
            ToastUtil.makeShortText(this, "清除缓存成功");
        } else if (type == 3) {
            UserInfoBean.getInstance().clearUserInfo();
            AppManager.getAppManager().finishAllActivityAndExit();
            JPushInterface.setAlias(this, "", null);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            this.finish();
        }
    }
}
